package com.sitanyun.merchant.guide.weiht;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * Created by jzl
 * on 2022/2/28.
 */
public class Common {
    /**
     3      * 获取版本号
     4      *
     5      * @return 当前应用的版本号
     6      */
     public static String getVersion(Context context) {
                try {
                        PackageManager manager = context.getPackageManager();
                         PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                                         0);
                         String version = info.versionName;
                       return version;
                   } catch (Exception e) {
                        e.printStackTrace();
                        return "";
                   }
             }

                /**
 21      * 版本号比较
 22      *
 23      * @param version1
 24      * @param version2
 25      * @return
 26      */
             public static int compareVersion(String version1, String version2) {
                 if (version1.equals(version2)) {
                        return 0;
                    }
                String[] version1Array = version1.split("\\.");
                String[] version2Array = version2.split("\\.");

               int index = 0;
                // 获取最小长度值
                int minLen = Math.min(version1Array.length, version2Array.length);
                int diff = 0;
                // 循环判断每位的大小
                 Log.d("HomePageActivity", "verTag2=2222="+version1Array[index]);
                 while (index < minLen
                        && (diff = Integer.parseInt(version1Array[index])
                        - Integer.parseInt(version2Array[index])) == 0) {
                        index++;
                   }
                if (diff == 0) {
                        // 如果位数不一致，比较多余位数
                        for (int i = index; i < version1Array.length; i++) {
                                if (Integer.parseInt(version1Array[i]) > 0) {
                                        return 1;
                                   }
                           }
                       for (int i = index; i < version2Array.length; i++) {
                                if (Integer.parseInt(version2Array[i]) > 0) {
                                       return -1;
                                   }
                          }
                        return 0;
                    } else {
                        return diff > 0 ? 1 : -1;
                   }
             }

        }

